package com.sinog.core.util;

import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * @desc map转换工具包(map和实体类互转)
 * @author shily
 * @date 2014-8-12 21:12:10
 */
public final class MapUtil {

    /**
     * 将Map转化为Map<String,String>（null置为""）
     * @param map 待转换Map
     * @return Map<String,String>
     */
    public static Map<String,String> objectMapToStringMap(Map<String,String> map) {
        Map<String,String> resultMap = new HashMap<>(16);
        for(String obj : map.keySet()) {
            resultMap.put(obj,null == map.get(obj) ? "" : map.get(obj));
        }
        return resultMap;
    }

    /**
     * map转string
     * @param map map
     * @return Map<String,Object>
     */
    public static Map<String,Object> mapToString(Map<String,Object> map) {
        Map<String,Object> result = new HashMap<>(16);
        for(String s : map.keySet()) {
            //取出key
            String key = s.toString();
            //通过key拿到value
            String val = map.get(key).toString();
            result.put(key,val);
        }
        return result;
    }

    /**
     * 页面请求参数转换,原参数"-1",转为null
     * @param map 请求Map
     * @return Map<String,Object> 转换map
     */
    public static Map<String,Object> requestConversionToMap(Map<String,Object> map) {
        for(Map.Entry<String,Object> entry : map.entrySet()) {
            if(StringUtils.equals(entry.getValue().toString(),"-1")) {
                map.put(entry.getKey(),null);
            }
        }
        return map;
    }

    /**
     * 实体类转map
     * @param object 实体类转Map
     * @return Map<String, Object>
     */
    public static Map<String, Object> entityToMap(Object object) {
        Map<String, Object> map = new HashMap<>(16);
        for (Field field : object.getClass().getDeclaredFields()) {
            try {
                boolean flag = field.isAccessible();
                field.setAccessible(true);
                Object o = field.get(object);
                map.put(field.getName(), o);
                field.setAccessible(flag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    /**
     * map转换为实体类
     * @param map 需要初始化的数据，key字段必须与实体类的成员名字一样，否则赋值为空
     * @param entity 需要转化成的实体类
     * @return T
     */
    public static <T> T mapToEntity(Map<String, Object> map, Class<T> entity) {
        T t = null;
        try {
            t = entity.newInstance();
            for (Field field : entity.getDeclaredFields()) {
                if (map.containsKey(field.getName())) {
                    boolean flag = field.isAccessible();
                    field.setAccessible(true);
                    Object object = map.get(field.getName());
                    if (object != null && field.getType().isAssignableFrom(object.getClass())) {
                        field.set(t, object);
                    }
                    field.setAccessible(flag);
                }
            }
            return t;
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * 将Map中的值赋值到另一个map中
     * @param fromMap  fromMap
     * @param toMap 把fromMap中的值赋值到toMap中
     * @param params 赋值的key
     */
    public static void setValueMapToMap(Map<String,Object> fromMap,Map<String,Object> toMap,String ... params) {
        for(String param : params) {
            toMap.put(param,fromMap.get(param));
        }
    }
}